import { computed, inject, ref, unref } from "vue";
import { formContextKey, formItemContextKey } from "./form/constants";
import type { MaybeRef } from "@vueuse/core";
import type { ComponentSize } from "./size";
import { useProp } from "./useProp";
import { useGlobalSize } from "./useSize";

export const useFormDisabled = (fallback?: MaybeRef<boolean | undefined>) => {
  const disabled = useProp<boolean>("disabled");
  const form = inject(formContextKey, undefined);
  return computed(
    () => disabled.value || unref(fallback) || form?.disabled || false
  );
};

// eslint-disable-next-line sort-exports/sort-exports
export const useDisabled = useFormDisabled;

export const useFormSize = (
  fallback?: MaybeRef<ComponentSize | undefined>,
  ignore: Partial<Record<"prop" | "form" | "formItem" | "global", boolean>> = {}
) => {
  const emptyRef = ref(undefined);

  const size = ignore.prop ? emptyRef : useProp<ComponentSize>("size");
  const globalConfig = ignore.global ? emptyRef : useGlobalSize();
  const form = ignore.form
    ? { size: undefined }
    : inject(formContextKey, undefined);
  const formItem = ignore.formItem
    ? { size: undefined }
    : inject(formItemContextKey, undefined);

  return computed(
    (): ComponentSize =>
      size.value ||
      unref(fallback) ||
      formItem?.size ||
      form?.size ||
      globalConfig.value ||
      ""
  );
};
// These exports are used for preventing breaking changes
export const useSize = useFormSize;
